package com.yuma.springaitool.controller;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import java.util.Map;

@RestController
public class MultiModelsController {

    @Autowired @Qualifier("workChatClient")
    ChatClient workChatClient;

    @Autowired @Qualifier("botChatClient")
    ChatClient botChatClient;

    public class AiJob {
        record Job(JobType jobType, Map<String,String> keyInfos) {
        }
        public enum JobType{
            TECHNOLOGY,
            CONSULT,
            OTHER,
        }
    }

    @GetMapping(value = "/call", produces = "text/stream;charset=UTF8")
    String call(@RequestParam String message) {
        AiJob.Job job = workChatClient.prompt().user(message).call().entity(AiJob.Job.class);

        switch (job.jobType()){
            //http://localhost:10006/call?message=构造方法是什么
            case TECHNOLOGY ->{
                if(job.keyInfos().size()==0){
                    return "学员需要提供姓名和班级号";
                } else {
                    //http://localhost:10006/call?message=我是张三，JAVA01班，构造方法是什么
                    return job.keyInfos.toString();
                }
            }
            //http://localhost:10006/call?message=java方向
            case CONSULT -> {
                if(job.keyInfos().size()==0){
                    return "学员需要提供报名学习相关方向";
                }
                else {
                    //http://localhost:10006/call?message=java学费多少
                    return job.keyInfos.toString();
                }
            }
            case OTHER -> {
                //http://localhost:10006/call?message=你好
                return botChatClient.prompt().user(message).call().content();
            }
            default -> {
                return "解析失败";
            }
        }
    }

    @GetMapping(value = "/stream", produces = "text/stream;charset=UTF8")
    Flux<String> stream(@RequestParam String message) {
        AiJob.Job job = workChatClient.prompt().user(message).call().entity(AiJob.Job.class);
        return botChatClient.prompt().user(message).stream().content();
    }
}